package com.demo.asyncs;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.demo.model.NavigationDataSet;
import com.demo.model.Placemark;

@SuppressWarnings("unused")
public class NavigationSaxHandler extends DefaultHandler {

	private KmlParserListener kmlParserListener;

	private boolean in_kmltag = false;
	private boolean in_placemarktag = false;
	private boolean in_nametag = false;
	private boolean in_descriptiontag = false;
	private boolean in_geometrycollectiontag = false;
	private boolean in_linestringtag = false;
	private boolean in_pointtag = false;
	private boolean in_coordinatestag = false;
	private StringBuffer buffer;

	private Placemark placemark;

	private NavigationDataSet navigationDataSet;

	@Override
	public void startDocument() throws SAXException {
		this.navigationDataSet = new NavigationDataSet();
	}

	@Override
	public void endDocument() throws SAXException {

	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {

		if (localName.equals("kml")) {
			this.in_kmltag = true;
		} else if (localName.equals("Placemark")) {
			this.in_placemarktag = true;
			placemark = new Placemark();
		} else if (placemark != null) {
			if (localName.equals("name")) {
				this.in_nametag = true;
			} else if (localName.equals("description")) {
				this.in_descriptiontag = true;
			} else if (localName.equals("GeometryCollection")) {
				this.in_geometrycollectiontag = true;
			} else if (localName.equals("LineString")) {
				this.in_linestringtag = true;
				placemark.setPoint(false);
			} else if (localName.equals("point")) {
				this.in_pointtag = true;
				placemark.setPoint(true);
			} else if (localName.equals("coordinates")) {
				buffer = new StringBuffer();
				this.in_coordinatestag = true;
			}
		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("kml")) {
			this.in_kmltag = false;
		} else if (localName.equals("Placemark")) {
			this.in_placemarktag = false;
			if (placemark != null) {				
				if (!placemark.getCoordinates().equals("")) {
					getNavigationDataSet().add(placemark);
					
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					if (kmlParserListener != null) {
						kmlParserListener.OnPointParsed(placemark);
					}
				}
				placemark = null;
			}
		} else if (localName.equals("name")) {
			this.in_nametag = false;
			if (placemark != null) {
				placemark.setTitle(buffer.toString());
			}
		} else if (localName.equals("description")) {
			this.in_descriptiontag = false;
			if (placemark != null) {
				placemark.setDescription(buffer.toString());
			}
		} else if (localName.equals("GeometryCollection")) {
			this.in_geometrycollectiontag = false;
		} else if (localName.equals("LineString")) {
			this.in_linestringtag = false;
		} else if (localName.equals("point")) {
			this.in_pointtag = false;
		} else if (localName.equals("coordinates")) {
			this.in_coordinatestag = false;
			if (placemark != null) {
				placemark.setCoordinates(buffer.toString().trim());
			}
		}
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		buffer = new StringBuffer(new String(ch, start, length));
	}

	public NavigationDataSet getNavigationDataSet() {
		return navigationDataSet;
	}

	public void setNavigationDataSet(NavigationDataSet navigationDataSet) {
		this.navigationDataSet = navigationDataSet;
	}

	public KmlParserListener getKmlParserListener() {
		return kmlParserListener;
	}

	public void setKmlParserListener(KmlParserListener kmlParserListener) {
		this.kmlParserListener = kmlParserListener;
	}
}